package com.psp.park.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.northend.util.AppTextUtil;
import com.northend.util.StringUtil;
import com.psp.park.controller.res.bean.RInvoiceHeaderDetailBean;
import com.psp.park.controller.res.bean.RInvoiceHeaderListBean;
import com.psp.park.model.InvoiceHeaderBean;
import com.psp.park.persist.dao.InvoiceHeaderDao;
import com.psp.park.service.InvoiceService;
import com.psp.park.service.exception.ServiceException;

@Service
public class InvoiceServiceImpl implements InvoiceService {

	@Autowired
	InvoiceHeaderDao invoiceHeaderImpl;

	@Override
	public PageResult<RInvoiceHeaderListBean> getHeaderList(int page, int pageSize, String puid, String uid) {
		PageResult<RInvoiceHeaderListBean> result = new PageResult<RInvoiceHeaderListBean>();
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}

		int count = invoiceHeaderImpl.selectCount(uid);
		List<InvoiceHeaderBean> resList = invoiceHeaderImpl.selectList(page, pageSize, uid);

		List<RInvoiceHeaderListBean> resData = new ArrayList<>();
		if (resList != null && resList.size() > 0) {
			for (InvoiceHeaderBean bean : resList) {
				RInvoiceHeaderListBean rb = new RInvoiceHeaderListBean();
				rb.setId(bean.getId());
				if (bean.getType() != null) {
					if (bean.getType() == 1) {
						rb.setType("个人");
					} else if (bean.getType() == 2) {
						rb.setType("企业");
					}
				}
				rb.setTitle(bean.getTitle());
				resData.add(rb);
			}
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}

	@Override
	public RInvoiceHeaderDetailBean getHeaderDetail(String id, String puid) throws ServiceException {
		RInvoiceHeaderDetailBean result = new RInvoiceHeaderDetailBean();
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		InvoiceHeaderBean bean = invoiceHeaderImpl.selectById(id);
		if (bean != null) {
			result.setId(bean.getId());
			result.setTitle(bean.getTitle());
			result.setType(bean.getType());
			return result;
		}
		return null;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean saveHeader(String id, String puid, String uid, String title, Integer type) throws ServiceException {
		boolean flag = false;
		boolean isNew = StringUtil.isEmpty(id);

		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}

		InvoiceHeaderBean bean = new InvoiceHeaderBean();
		bean.setId(id);
		bean.setTitle(title);
		bean.setType(type);
		bean.setUid(uid);
		if (isNew) {
			bean.setId(AppTextUtil.getPrimaryKey());
			bean.setCreateDate(new Date());
			flag = invoiceHeaderImpl.insert(bean) > 0;
		} else {
			flag = invoiceHeaderImpl.update(bean) > 0;
		}

		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean deleteHeader(String puid, String id) throws ServiceException {
		boolean flag = false;
		flag = invoiceHeaderImpl.deleteById(id) > 0;
		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

}
